/*
   Copyright 2008, Andres Postiglioni

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package com.aconcagua.samples.charts.state.model {
  import flash.events.Event;
  import flash.events.EventDispatcher;

  /**
   * @author Andres Postiglioni
   */
  [Event(name="sliderChanged")]
  public class Slider extends EventDispatcher {
    private var _zoomFrom: Number;
    private var _zoomTo: Number;
    private var _leftBoundary: Date;
    private var _rightBoundary: Date;
    private var _series: Series;

    public function Slider(series: Series) {
      _series = series;

      _leftBoundary = series.getElementAt(0).date;
      _rightBoundary = series.getElementAt(series.length - 1).date;
      _zoomFrom = _leftBoundary.time;
      _zoomTo = _rightBoundary.time;
    }

    [Bindable(event="sliderChanged")]
    public function get series(): Series {
      return _series;
    }

    public function pointToTime(pointInTime: Number): Number {
      return _leftBoundary.time + pointInTime;
    }

    public function pointToDate(point: Number): Date {
      return new Date(this.pointToTime(point));
    }

    public function get leftBoundary(): Date {
      return _leftBoundary;
    }

    public function get rightBoundary(): Date {
      return _rightBoundary;
    }

    [Bindable(event="sliderChanged")]
    public function get zoomFrom(): Date {
      return new Date(_zoomFrom);
    }

    [Bindable(event="sliderChanged")]
    public function get zoomTo(): Date {
      return new Date(_zoomTo);
    }

    [Bindable(event="sliderChanged")]
    public function get zoomPeriodSpan(): Number {
      return Math.max(0, _zoomTo - _zoomFrom);
    }

    [Bindable(event="sliderChanged")]
    public function get totalSpan(): Number {
      return _rightBoundary.time - _leftBoundary.time;
    }

    [Bindable(event="sliderChanged")]
    public function get rightPeriodSpan(): Number {
      return Math.max(0, _rightBoundary.time - _zoomTo);
    }

    [Bindable(event="sliderChanged")]
    public function get leftPeriodSpan(): Number {
      return Math.max(0, _zoomFrom - _leftBoundary.time);
    }

    [Bindable(event="sliderChanged")]
    public function get leftPeriodSpanPercent(): Number {
      return leftPeriodSpan / totalSpan;
    }

    [Bindable(event="sliderChanged")]
    public function get rightPeriodSpanPercent(): Number {
      return rightPeriodSpan / totalSpan;
    }

    [Bindable(event="sliderChanged")]
    public function get zoomPeriodSpanPercent(): Number {
      return zoomPeriodSpan / totalSpan;
    }

    public function zoom(timeFrom: Number, timeTo: Number): void {
      _zoomFrom = timeFrom;
      _zoomTo = timeTo;
    }
  }
}